import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        int[] arr={4,2,3,4};
        System.out.println(triangleNumber(arr));

    }
    private static void sort(int[]nums){

        for(int i = 1 ; i < nums.length ; i ++ ){
            int tmp = nums[i];
            int j = i - 1 ;
            while(j >= 0){
                if(tmp < nums[j]){
                    nums[j+1] = nums[j];
                }else{
                    break ;
                }
                j -- ;
            }
            nums[j+1] = tmp ;
        }
    }

    public static int triangleNumber(int[] nums) {
        if(nums == null)return 0;
        sort(nums);
        int sum = 0 ;
        for(int i = nums.length-1 ; i >=2 ; i --){
            int two = i-1 ;
            int one = 0 ;
            while(one < two){
                if(nums[one]+nums[two] > nums[i]){
                    sum += two-one ;
                    two -- ;
                }else{
                    one ++ ;
                }
            }

        }
        return sum ;
    }
}